﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using APACWP8.String;

namespace APACWP8.View
{
    public partial class SearchPage : PhoneApplicationPage
    {
        public SearchPage()
        {
            InitializeComponent();
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            CheckOrientation(LayoutRoot);
        }
        private void CheckOrientation(Grid content)
        {

            if ((Orientation & PageOrientation.Portrait) == (PageOrientation.Portrait))
            {
                content.Margin = new Thickness(0, 0, 0, 0);
            }
            else if ((Orientation & PageOrientation.LandscapeLeft) == (PageOrientation.LandscapeLeft))
            {
                content.Margin = new Thickness(0, 0, 36, 0);
            }
            else if ((Orientation & PageOrientation.LandscapeRight) == (PageOrientation.LandscapeRight))
            {
                content.Margin = new Thickness(36, 0, 0, 0);
            }
        }
        private void PhoneTextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            var queryText = txtSearch.Text.Trim();
            if (queryText == "" || queryText.Length == 0)
                return;
            List<Datas> res = new List<Datas>();
            foreach (Datas dt in Searching.Searchs)
            {
               
                    if (dt.title.ToLower().Contains(queryText.ToLower()) || dt.hotkey.ToLower().Contains(queryText.ToLower()))
                    {
                        res.Add(dt);
                    }
                
            }
            if (res.Count != 0)
                listSearch.ItemsSource = res;
            else
                listSearch.ItemsSource = null;
        }

        private void txtSearch_ActionIconTapped(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            listSearch.ItemsSource = null;
        }

        private void listSearch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (listSearch.SelectedIndex == -1)
                return;
            var dataSelect = listSearch.SelectedItem as Datas;
            App.RootFrame.Navigate(new Uri("/View/" + dataSelect.ClassType + ".xaml", UriKind.Relative));
            listSearch.SelectedIndex = -1;

        }

        private void txtSearch_LostFocus(object sender, RoutedEventArgs e)
        {
            this.Focus();
        }

        private void listSearch_GotFocus(object sender, RoutedEventArgs e)
        {
            this.Focus();
        }
    }
}